package com.example.dao.impl;

import com.example.dao.AdminDao;
import com.example.model.Admin;
import com.example.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin getAdminByUsername(String username) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,admin,apassword from tb_admin where admin=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            while (rs.next()){
                admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setAdmin(rs.getString("admin"));
                admin.setApassword(rs.getString("apassword"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return admin;
    }
}
